//
//  DialogView.swift
//  sss
//
//  Created by hrrMac on 2020/6/2.
//  Copyright © 2020 hrrMac. All rights reserved.
//

import Foundation

///弹出方式
enum DialogViewShowType {
    case bottom  //底部弹出
    case center  //中间弹出
}




class DialogView: UIView{
    ///背景视图
    let bgView: UIView = UIView();
    
    ///弹出视图
    var dialogView: UIView = UIView();
    
    ///弹出位置
    var type: DialogViewShowType = .center
    
    ///点击空白区域是否消失
    var clickEmptyAreaDimiss = true
    
    var bottomSafeAreaView :UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(_ dialogView: UIView , type: DialogViewShowType = .center){
        self.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        self.dialogView = dialogView
        self.type = type
        configUI()
        refreshView()
    }
    
    
    
    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: coder);
        configUI()
    }
    
    
    private func configUI(){
        ///背景视图
        bgView.backgroundColor = UIColor.black
        bgView.frame = self.bounds
        bgView.alpha = 0.0
        self.addSubview(bgView)
        let tapRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(clickEmptyArea))
        bgView.addGestureRecognizer(tapRecognizer)
    }
    
    
    func refreshView(){
        if type == .bottom {
            if #available(iOS 11.0, *) {
//                print(self.safeAreaInsets.bottom)
                bottomSafeAreaView = UIView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - self.safeAreaInsets.bottom - 10, width: UIScreen.main.bounds.width, height: self.safeAreaInsets.bottom + 10))
                bottomSafeAreaView?.backgroundColor = UIColor.white
                addSubview(bottomSafeAreaView!)
            }
            dialogView.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: dialogView.frame.size.width, height: dialogView.frame.size.height)
        }
        self.addSubview(dialogView)
    }
    
    // self.safeAreaInsets.bottom 上面没有生效
    override func layoutSubviews() {
        if type == .bottom {
            if #available(iOS 11.0, *) {
                print(self.safeAreaInsets.bottom)
                bottomSafeAreaView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - self.safeAreaInsets.bottom - 10, width: UIScreen.main.bounds.width, height: self.safeAreaInsets.bottom + 10)
            }
        }
    }
    
}


extension DialogView{
    
    @discardableResult
    static func showDilogiView(_ dialogView: UIView, type:DialogViewShowType = .center, isShow:Bool = true) -> DialogView{
        let dialogView = DialogView(dialogView, type: type)
        if isShow {
            dialogView.show()
        }
        return dialogView
    }
}



///Action
extension DialogView{
    
    func show(){
        UIApplication.shared.keyWindow?.addSubview(self)
        
        if type == .center {
            dialogView.center = self.center
            dialogView.transform = CGAffineTransform(scaleX: 1.2,y: 1.2)
            UIView.animate(withDuration: 0.2) {
                self.dialogView.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)
                self.bgView.alpha = 0.4
            }
            return
        }
        
        UIView.animate(withDuration: 0.2) {
            var frame = self.dialogView.frame
            if #available(iOS 11.0, *) {
                frame.origin.y = self.frame.size.height - self.dialogView.frame.size.height - self.safeAreaInsets.bottom
                self.bottomSafeAreaView?.isHidden = false
            } else {
                frame.origin.y = self.frame.size.height - self.dialogView.frame.size.height
            }
            self.dialogView.frame = frame
            self.bgView.alpha = 0.2
        }
    }
    
    
    func dismiss(){
        if type == .center {
            UIView.animate(withDuration: 0.2, animations: {
                self.dialogView.alpha = 0.0
                self.bgView.alpha = 0.0
            }) { (finished) in
                self.removeFromSuperview()
                self.dialogView.alpha = 1.0
                self.bgView.alpha = 0.4
            }
            return
        }
        
        UIView.animate(withDuration: 0.2, animations: {
            var frame = self.dialogView.frame
            frame.origin.y = self.frame.size.height//frame.origin.y + self.dialogView.frame.size.height
            self.dialogView.frame = frame
            self.bgView.alpha = 0.0

        }) { (finished) in
            self.bottomSafeAreaView?.isHidden = true
            self.removeFromSuperview()
        }
    }
    
    
    
      ///点击空白区域
    @objc func clickEmptyArea(){
        if clickEmptyAreaDimiss {
            dismiss()
        }
            
    }
        
    
}

